/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package poetry.gui

import poetry.parser.{Poetry, Strophe}
import scala.swing._
import event._
import java.awt.Font._
import javax.swing.KeyStroke

class MainComponent(private val poetry: Poetry, private val fSize: Int, private val edit: Boolean)
  extends BorderPanel {

  def this(fSize: Int, edit: Boolean) = this(new Poetry("",Nil),fSize,edit)
  def fontSize = fSize
  def editable = edit
  def data = poetry
  def toPoetry = {
    val strophs = bodyArea.text.split("\n\n").toList
    val poetry = strophs.map( x => new Strophe(x.split("\n").toList))
    new Poetry(nameField.text, poetry)
  }

  border = Swing.EmptyBorder(10, 10, 10, 10)
  val nameField = new TextField {
    if (edit) background = new Color(255,255,255)
    else {
      border = Swing.EmptyBorder(3)
      background = new Color(237,237,237)
    }
    font = new Font("sylfaen", BOLD, fSize)
    editable = edit
    text = poetry.name
  }
  val bodyArea = new ScrollPane {
    def text = textArea.text
    preferredSize = new Dimension(475,300)
    if (edit) border = nameField.border
    val textArea = new TextArea {
      if (edit) background = new Color(255,255,255)
      else background = new Color(237,237,237)
      font = new Font("sylfaen", BOLD, fSize)
      editable = edit
      text = poetry.bodyString
    }
    contents = textArea
  }
  layout(nameField) = BorderPanel.Position.North
  layout(bodyArea) = BorderPanel.Position.Center
}

class MyTextArea(private val cont: String, private val fSize: Int) extends TextArea {
  background = new Color(237,237,237)
  font = new Font("sylfaen", PLAIN, fSize)
  text = cont
  def fontSize = fSize
}

class MyScrollPane extends ScrollPane {
  def getContent = contents.last.asInstanceOf[MainComponent]
}

class MyLabel(private val label: String, private val fSize: Int) extends Label(label) {
  font = new Font("sylfaen", PLAIN, fSize)
  def fontSize = fSize
}

class MyMenu(private val label: String) extends Menu(label) {
  font = new Font("sylfaen", PLAIN, 12)
}

class MyMenuItem(override val action: Action) extends MenuItem(action) {
  font = new Font("sylfaen", PLAIN, 12)
  border = Swing.EmptyBorder(5,3,5,3)

  def accelerator = this.peer.getAccelerator
  def accelerator_=(key: (Int, Int)) =
        this.peer.setAccelerator(KeyStroke.getKeyStroke(key._1, key._2))
}

class MyMessage(message: String) extends Dialog {
  resizable = false
  location = new Point(370,170)
  title = "შეტყობინება"
  defaultButton = new Button(Action("დახურვა") { close }) {
    font = new Font("sylfaen", BOLD, 12)
    listenTo(keys)
    reactions += {
      case e: KeyPressed if e.key == Key.Enter => this.doClick
    }
  }
  contents = new GridBagPanel {
    border = Swing.EmptyBorder(10, 10, 10, 10)
    val c = new Constraints
    c.grid = (0,0)
    layout(new TextArea(message) {
      font = new Font("sylfaen", BOLD, 12)
      background = new Color(237,237,237)
      editable = false
    }) = c
    c.insets.set(15,5,5,5)
    c.grid = (0,1)
    layout(defaultButton.get) = c
  }
}

class MyConfirm(message: String) extends Dialog {
  resizable = false
  location = new Point(370,170)
  title = "შეტყობინება"
  defaultButton = new Button(Action("არა") { close }) {
    font = new Font("sylfaen", BOLD, 12)
    listenTo(keys)
    reactions += {
      case e: KeyPressed if e.key == Key.Enter => this.doClick
    }
  }
  contents = new GridBagPanel {
    border = Swing.EmptyBorder(10, 10, 10, 10)
    val c = new Constraints
    c.grid = (0,0)
    c.gridwidth = 4
    layout(new TextArea(message) {
      font = new Font("sylfaen", BOLD, 12)
      background = new Color(237,237,237)
      editable = false
    }) = c
    c.insets.set(15,150,5,5)
    c.gridwidth = 1
    c.grid = (1,1)
    layout(new Button(Action("დიახ") { System.exit(0) }) {
      font = new Font("sylfaen", BOLD, 12)
      listenTo(keys)
      reactions += {
        case e: KeyPressed if e.key == Key.Enter => this.doClick
      }
    }) = c
    c.grid = (2,1)
    c.insets.set(15,5,5,150)
    layout(defaultButton.get) = c
  }
}